home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * NSSDC/CDF CDF character string handling.
- *
- * Version 1.3, 10-Feb-92, ST Systems (STX)
- *
- * Modification history:
- *
- * V1.0 16-May-91, J Love Original version (for CDF V2.1). This is a
- * combination of nulterminate.c, isdescr.c,
- * descrtoref.c, and descrtorefnul.c.
- * V1.1 31-Jul-91, J Love Added 'array' class to checking for a
- * descriptor (DSC$K_CLASS_A).
- * V1.2 23-Sep-91, J Love Modified for IBM-PC port.
- * V1.3 10-Feb-92, J Love CDF V2.2.
- *
- ******************************************************************************/
-
- #include "cdflib.h"
-
- /******************************************************************************
- * NOTE:
- * Never assume that a string may be NUL-terminated already. If it is,
- * it might be accidental and the NUL might go away.
- ******************************************************************************/
-
- #define NOMINAL_CDF_STRING_LENGTH (128+1)
- #define MAX_CDF_CALL_STRINGS 10 /* maximum available strings
- per CDF call */
-
-
- /******************************************************************************
- * NUL-terminate a character string.
- ******************************************************************************/
-
- char *NULterminate (string, length)
- char *string; /* string to NUL-terminate */
- size_t length; /* length of string */
- {
- long string_n; /* string to use */
- char *string_ptr; /* pointer to string being used to pass
- by reference */
-
- static long next_string_n = 0; /* next string to use */
-
- static char strings[MAX_CDF_CALL_STRINGS][NOMINAL_CDF_STRING_LENGTH+1];
- static char *aux_strings[MAX_CDF_CALL_STRINGS];
- static size_t aux_lengths[MAX_CDF_CALL_STRINGS] =
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-
- string_n = next_string_n;
- next_string_n = (next_string_n + 1) % MAX_CDF_CALL_STRINGS;
-
- if (length <= NOMINAL_CDF_STRING_LENGTH)
- string_ptr = strings[string_n];
- else
- if (length <= aux_lengths[string_n])
- string_ptr = aux_strings[string_n];
- else {
- if (aux_strings[string_n] != NULL) free (aux_strings[string_n]);
- aux_strings[string_n] = (char *) malloc (length + 1);
- if (aux_strings[string_n] == NULL) return NULL; /* Very bad. */
- aux_lengths[string_n] = length;
- string_ptr = aux_strings[string_n];
- }
-
- strncpy (string_ptr, string, length); /* C will never look at anything past
- the NUL so don't copy past it like
- 'memmove' would */
- *(string_ptr + length) = NUL; /* still NUL terminate in case 'string'
- didn't contain a NUL before 'length'
- was reached */
-
- return string_ptr;
- }
-
-
- #if defined(vms)
- /******************************************************************************
- * Is a descriptor being pointed to?
- *
- * NOTE:
- * There is no checking on the length of the string because all lengths
- * are valid to VMS and to CDF (for attributes with character data types).
- ******************************************************************************/
-
- #include <descrip.h>
-
- Boolean isDESCR (ptr, aptr, len)
- void *ptr; /* could point to a descriptor or to the
- string (if already passed by reference) */
- char **aptr; /* pointer to string */
- size_t *len; /* length of string */
- {
- struct dsc$descriptor *Tptr = (struct dsc$descriptor *) ptr;
-
- if (Tptr->dsc$b_dtype == DSC$K_DTYPE_T &&
- (Tptr->dsc$b_class == DSC$K_CLASS_S ||
- Tptr->dsc$b_class == DSC$K_CLASS_A)) {
- *len = Tptr->dsc$w_length;
- *aptr = Tptr->dsc$a_pointer;
- return TRUE;
- }
- else
- return FALSE;
-
- }
- #endif
-
-
- #if defined(vms)
- /******************************************************************************
- * Convert from passing by descriptor to passing by reference.
- ******************************************************************************/
-
- char *DESCRtoREF (ptr)
- void *ptr; /* could point to a descriptor or to the string (if
- already passed by reference) */
- {
- char *aptr; /* pointer to string */
- size_t len; /* length of string */
-
- if (isDESCR (ptr, &aptr, &len))
- return aptr;
- else
- return (char *) ptr; /* already passed by reference */
-
- }
- #endif
-
-
- #if defined(vms)
- /******************************************************************************
- * Convert from passing by descriptor to passing by reference and
- * NUL-terminate.
- *
- * NOTE:
- * If a descriptor is being pointed to, always use a temporary string to pass
- * by reference - never assume that the string may already be NUL-terminated
- * (in case its NUL-terminated by chance and the NUL might go away).
- *
- ******************************************************************************/
-
- char *DESCRtoREFnul (ptr, maxREFlen)
- void *ptr; /* could point to a descriptor or to the string (if
- already passed by reference) */
- size_t maxREFlen; /* maximum length of the string if passed by
- reference */
- {
- char *aptr; /* pointer to string */
- size_t len; /* length of string */
-
- if (isDESCR (ptr, &aptr, &len))
- return NULterminate (aptr, len);
- else
- return NULterminate (ptr, maxREFlen);
- }
- #endif
-